package com.sunday.aomicaichang.ui.product;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.member.utils.SignUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/13.
 */

public class DataDetailActivity extends BaseActivity {
    @Bind(R.id.tit)
    TextView tit;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.btn_collect)
    ImageView btnCollect;
    @Bind(R.id.img)
    ImageView img;
    @Bind(R.id.web_view)
    WebView webView;

    private String url,tits,times,imgs,viceName;
    private boolean isCollect;
    private long id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_detail);
        ButterKnife.bind(this);
        url= getIntent().getStringExtra("url");
        tits= getIntent().getStringExtra("tit");
        times= getIntent().getStringExtra("time");
        imgs=getIntent().getStringExtra("img");
        viceName= getIntent().getStringExtra("viceName");
        id= getIntent().getLongExtra("id",0);
        if(tits!=null)
        tit.setText(tits);
        if(viceName!=null||times!=null)
        time.setText(viceName+""+times);
        if(img!=null)
        Picasso.with(mContext)
                .load(imgs)
                .error(R.mipmap.ic_product)
                .into(img);
        //启用支持javascript
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(false);
        settings.setSupportZoom(false);
        settings.setDomStorageEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!TextUtils.isEmpty(url)) {
                    view.loadUrl(url);
                } else {
                    dissMissDialog();
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dissMissDialog();
            }
        });
        webView.loadUrl(url);

        btnCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnCollect.setSelected(!isCollect);
                isCollect = !isCollect;
                collect();
            }
        });
    }
    private void collect() {
        String nonce = MD5Utils.random();
        String[] str1 = new String[]{"type","otherId", "nonce"};
        String[] str2 = new String[]{String.valueOf(3),String.valueOf(id), nonce};
        Call<ResultDO> call = ApiClient.getApiAdapter().save(3, id, nonce, SignUtil.getSign(str1, str2));
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (response.body().getResult() == null) {
                        return;
                    }
                    if (isCollect) {
                        ToastUtils.showToast(mContext,"已收藏");
                    }else {
                        ToastUtils.showToast(mContext,"已取消收藏");
                    }
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
            }
        });
    }
}
